package com.mars.app.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by pengshuo on 2018/2/2 9:12
 * Description: 时间工具类
 * Modified By:
 * Version:
 *
 * @author pengshuo
 */
public class DateUtil {

    /**
     * 时间转换器
     */
    final static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

    /**
     * 日历
     */
    final static Calendar calendar= Calendar.getInstance();

    /**
     * 获取时间 20180202101111
     * @return 20180202101111
     */
    public static String timestamp(){
        return sdf.format(new Date());
    }

    /**
     * 获取current time的毫秒
     * @return long
     */
    public static long current(){
        return System.currentTimeMillis();
    }

    /**
     * 获取当前天数 +- day 的 时间戳
     * @param day +- day
     * @return long
     */
    public static long diff(int day){
        calendar.add(Calendar.DATE,day);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当前天数 +- day 的 时间戳
     * @param now 当前时间
     * @param day +- day
     * @return long
     */
    public static long diff(long now,int day){
        calendar.setTimeInMillis(now);
        calendar.add(Calendar.DATE,day);
        return calendar.getTimeInMillis();
    }

    /**
     *  yyyyMMdd
     * @return
     */
    public static String yyyyMMdd(){
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter =
                DateTimeFormatter.ofPattern("yyyyMMdd");
        return date.format(formatter);
    }

    /**
     * yyyyMMddHHmmss
     * @return
     */
    public static String yyyyMMddHHmmss(){
        LocalDateTime date = LocalDateTime.now();
        DateTimeFormatter formatter =
                DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        return date.format(formatter);
    }

//    public static void main(String[] args) {
//        System.out.println(timestamp());
//        System.out.println(System.currentTimeMillis());
//        System.out.println(diff(1));
//        System.out.println(diff(System.currentTimeMillis(),1));
//    }

}
